// pages/index/index.js
import { get, post, getCity } from '../../utils/request.js'
import global from '../../utils/global.js'
import { getLocation, openLocation } from '../../utils/wxapi.js'
import { starCount, updateStart} from '../../utils/util.js'
let that

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list1:[],//医院列表 
    list2: [],//医院列表 
    currentTab: 0,
    latitude:24.88554,
    longitude:102.82147,
    page:1,
    type:0,
    height: '',
    code:"530100",
    // userInfo:{}
  },
  
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    that = this;
    console.log(options.code)
    if (options.code){
      that.setData({
        code: options.code
      })
    }
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          height: res.windowHeight - 50
        })
      }
    })
    that.getLocation(); 
  },  
  /**
    * 生命周期函数--监听页面显示
    */
  onShow: function () {
    that.getLocation();  
    that.getSellerList(that.data.page, that.data.type, that.data.latitude, that.data.longitude);
    console.log(wx.getStorageSync("useInfo"),"dsdsds")
    that.setData({
      userInfo: wx.getStorageSync("useInfo")
    })
  },

//  获取经纬度
  getLocation: function () {
    getLocation(result => {
      console.log(result)
      if (!result){
        wx.showToast({
          title: '位置定位中',
          icon: 'loading',
          duration: 1000
        })
        
      }else{
        that.getSellerList(that.data.page, that.data.type, result.latitude, result.longitude);
      }
      
      that.setData({
        latitude: result.latitude*1,
        longitude: result.longitude * 1,
      })
      if (wx.getStorageSync('selectCIty')) {
        that.setData({
          city: wx.getStorageSync('selectCIty')
        })
      } else {
        that.getCity(result.longitude, result.latitude)
      }
     
    })
    
  },
  // 城市定位
  getCity(lon,lat){
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=EZueXnIGhjGOet9icCe8FORGHdTj6a5H&location=' + lat + ',' + lon + '&output=json&coordtype=wgs84ll',
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: res => {
        wx.hideLoading()
        if (res.statusCode !== 200) {
          wx.showToast({
            title: '网络不给力！',
            icon: 'loading',
            duration: 2000
          })
        } else {
          // console.log(res.data.result)
          that.setData({
            city: res.data.result.addressComponent.city
          })
        }
      }
    })
  },
  
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        type: e.target.dataset.current,
        list1:[],
        list2:[],
        page:1
      })
    }
    that.getSellerList(1, e.target.dataset.current, that.data.latitude, that.data.longitude)
  },
  // 获取医院列表
  getSellerList: function (page, type, latitude, longitude){  
    let param = {
      page: that.data.page++,
      type: type,
      orderWay: "ASC",
      latitude: latitude,
      longitude: longitude,
      code:that.data.code
    }
    // console.log(param)
    post(global.apiUrl.sellerList, param, function (res) {
      if (!res.more){
        wx.showToast({
          title: '已经加载完数据',
          icon: 'loading',
          duration: 1000
        })

        return
      }

      if (type == "0"){
        let curArr = that.data.list1.concat(res.obj)
        updateStart(curArr)
        that.setData({
          list1: curArr
        })
        return
      }
      if (type == "1") {
        let curArr = that.data.list2.concat(res.obj)
        updateStart(curArr)
        that.setData({
          list2: curArr
        })
        return
      }
      
    })
  },

  
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    // 停止下拉动作
    wx.stopPullDownRefresh();

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  lower: function () {
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    // 页数+1
    that.getSellerList(that.data.page, that.data.type, that.data.latitude, that.data.longitude)
  },

// 去切换地址
  selectAddress:(e)=>{
    // console.log(e.currentTarget.dataset.city)
    wx.redirectTo({
      url: 'selectAddress/selectAddress?city=' + e.currentTarget.dataset.city + '&lat=' + that.data.latitude + '&long=' + that.data.longitude
    })
  },

  // 去个人中心
  goUser(){
    wx.navigateTo({
      url: '../userCenter/index'
    })
  },
  getPhoneNumber:function(e){
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '取消授权',
        success: function (res) { }
      })
    } else {
      that.getUserInfo(e.detail.encryptedData, e.detail.iv)
          wx.navigateTo({
            url: '../userCenter/index'
          })
    }
   
  },
  // 获取用户信息
  getUserInfo(encryptedData, ivData){
    // doLogin
    let param ={
      encryptedData,
      ivData,
      js_code: wx.getStorageSync('js_code')
    }
    console.log("什么情况")
    post(global.apiUrl.doLogin, param, function (res) {
      console.log(res.obj,"ceshiyixia")
      wx.setStorageSync("useInfo", res.obj)
    })
  },
  // 去医院住院
  listDetail: function (event){
    console.log(event.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../listDetail/index?id='+event.currentTarget.dataset.id
    })
  }
})